<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------

namespace plugins\qbt_admin_log\controller;

use cmf\controller\PluginBaseController;
use think\Db;

class AdminIndexController extends PluginBaseController
{

    protected function _initialize()
    {
        $adminId = cmf_get_current_admin_id();
        if (!empty($adminId)) {
            if (!$this->checkAccess($adminId)) {
                $this->error("您没有访问权限！");
            }
            $this->assign("admin_id", $adminId);
        } else {
            if ($this->request->isAjax()) {
                $this->error("您还没有登录！", url("admin/Public/login"));
            } else {
                header("Location:" . url("admin/Public/login"));
                exit();
            }
        }
    }

    /**
     * 操作日志
     * @adminMenu(
     *     'name'   => '操作日志',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '操作日志',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if(!empty($param['date'])){
            $startTime = strtotime($param['date']);
            $endTime   = strtotime($param['date'].' 23:59:59');
            $where['addtime'] = [['>= time', $startTime], ['<= time', $endTime]];
        }

        if(!empty($param['username'])){
            $where['user_name'] = ['like',"%{$param['username']}%"];
        }

        if(!empty($param['ip'])){
            $where['ip'] = ['like',"%{$param['ip']}%"];
        }

        if(!empty($param['keyword'])){
            $where['request|content'] = ['like',"%{$param['keyword']}%"];
        }

        $lists = Db::name('admin_log')->where($where)->order('addtime desc')->paginate(10);
        $page = $lists->render();
        $this->assign('page', $page);
        $lists = $lists->toArray();
        $lists = $lists['data'];
        
        $this->assign('lists', $lists);
        return $this->fetch('/admin_index');
    }

    /**
     *  检查后台用户访问权限
     * @param int $userId 后台用户id
     * @return boolean 检查通过返回true
     */
    private function checkAccess($userId)
    {
        // 如果用户id是1，则无需判断
        if ($userId == 1) {
            return true;
        }

        $pluginName = $this->request->param('_plugin');
        $controller = $this->request->param('_controller');
        $controller = cmf_parse_name($controller, 1);
        $action     = $this->request->param('_action');

        return cmf_auth_check($userId, "plugin/{$pluginName}/$controller/$action");
    }
}
